/**
 * 版权所有：厦门市巨龙信息科技有限公司
 * Copyright 2023 Xiamen Dragoninfo Eng. Co. Ltd.
 * All right reserved.
 */
package com.github.alfred.convert;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * 类说明
 *
 * @author chengh
 * @date 2023/7/17 10:48
 */

public interface MapConvertMapper {

    /**
     * mapToStr
     * @param map map
     * @return {@link String}
     */
    default String mapToStr(Map<String, Object> map) {
        if (MapUtil.isNotEmpty(map)) {
            return JSON.toJSONString(map);
        }
        return null;
    }

    /**
     * strToMap
     * @param json json
     * @return {@link Map<String,Object>}
     */
    default Map<String, Object> strToMap(String json) {
        if (CharSequenceUtil.isBlank(json)) {
            return Maps.newHashMap();
        }
        return JSON.parseObject(json);
    }

    /**
     * strListToMapList
     * @param jsonList  jsonList
     * @return {@link List<Map<String,Object>>}
     */
    default List<Map<String, Object>> strListToMapList(String jsonList) {
        if (CharSequenceUtil.isBlank(jsonList)) {
            return Lists.newArrayList();
        }
        return JSON.parseObject(jsonList, new TypeReference<List<Map<String, Object>>>() {
        });
    }

}
